<?php
/*
  currencies_batch_update.php, v 1.0, 2003/07/27
  Contribution for use with osCommerce, Open Source E-Commerce Solutions, http://www.oscommerce.com.

  Copyright (c) 2003 Pablo D'Ambrosio, pablo@customware.com.ar

  Released under the GNU General Public License
*/

if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
define('DIR_WS_INCLUDES', 'administrator/components/com_oscommerce/includes/');
define('DIR_WS_FUNCTIONS' , 'administrator/components/com_oscommerce/includes/functions/');
define('DB_SERVER' , $mosConfig_host );
define('DB_SERVER_USERNAME', $mosConfig_user);
define('DB_SERVER_PASSWORD', $mosConfig_password);
include ('components/com_oscommerce/includes/configure.php');
  // -- webMS Modification --
  // Check if osCommerce environment is already loaded
  if (defined( '_OSCOMMERCE_LOADED' )) {
    if (!DB_SHARED) {
      // Reconnect to osCommerce Database
      tep_db_connect() or die('Unable to connect to database server!');
    }
    // Load osCommerce Globals for Boxes
    global $category_links, $languages_id, $catID, $catID_array, $current_category_id;
    global $request_type, $lng, $cart, $currencies, $language, $navigation, $breadcrumb;
  } else {
    // Load osCommerce Environment for the First Time
    require('administrator/components/com_oscommerce/includes/application_top.php');
  }

require(DIR_WS_LANGUAGES . 'admin/' . $language . '/currencies.php');
 //define('CURRENCY_SERVER_PRIMARY', 'oanda');
  //define('CURRENCY_SERVER_BACKUP', 'xe');

// report to ?: 0=none, 1=screen, 2=email, other=both
if (isset($HTTP_GET_VARS['output_report'])) {
  $output_report = (int)$HTTP_GET_VARS['output_report'];
} else {
  $output_report = 1;
}

$report = '';

$server_used = CURRENCY_SERVER_PRIMARY;

$currency_query = tep_db_query("select currencies_id, code, title from " . TABLE_CURRENCIES);
while ($currency = tep_db_fetch_array($currency_query)) {
  $quote_function = 'quote_' . CURRENCY_SERVER_PRIMARY . '_currency';
  $rate = $quote_function($currency['code']);

  if (empty($rate) && (tep_not_null(CURRENCY_SERVER_BACKUP))) {
    $quote_function = 'quote_' . CURRENCY_SERVER_BACKUP . '_currency';
    $rate = $quote_function($currency['code']);

    $server_used = CURRENCY_SERVER_BACKUP;
  }

  if (tep_not_null($rate)) {
    tep_db_query("update " . TABLE_CURRENCIES . " set value = '" . $rate . "', last_updated = now() where currencies_id = '" . (int)$currency['currencies_id'] . "'");

    if ($output_report) {
      $report .= "\n" . $currency['code'] . ', ' . $server_used . ', success, ' . $rate;
    }
  } elseif ($output_report) {
    $report .= "\n" . $currency['code'] . ', ' . $server_used . ', error';
  }
}

// do report
if ($output_report != 0) {
  $report  = 'Updated on ' . date("D M j G:i:s Y") . "\n" . $report;

  if ($output_report != 2) {
    echo $report;
  }

  if ($output_report != 1) {
    tep_mail(STORE_NAME, STORE_OWNER_EMAIL_ADDRESS, 'Currency Rates updated - ' . STORE_NAME, stripslashes($report), STORE_NAME, STORE_OWNER_EMAIL_ADDRESS);
  }
}

  //require(DIR_WS_INCLUDES . 'application_bottom.php');

  // -- webMS Modification --
  // reset the Mambo Database connection (stomped by osCommerce Connection)
  if (!DB_SHARED) {
    global $database, $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix;
    $database = new database( $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix );
  }
 ?>